﻿using Shiloh.Job.Common.Extensions;
using Shiloh.Job.Model.Enums;
using SqlSugar;
using System.Text.RegularExpressions;
using Shiloh.Job.Common.Helper;
namespace Shiloh.Job.Api.Models
{
    public class LoginRequest
    {
        public LoginWayEnum Type { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public string Code { get; set; }
        public string WeChatCode { get; set; }
    }
    public interface ILoginValidator
    {
        bool Validate(LoginRequest request, out List<string> errors);
    }
    // 密码登录校验
    public class PasswordValidator : ILoginValidator
    {
        public bool Validate(LoginRequest request, out List<string> errors)
        {
            errors = new List<string>();
            if (string.IsNullOrEmpty(request.Username))
                errors.Add("用户名不能为空");
            if (string.IsNullOrEmpty(request.Password))
                errors.Add("密码不能为空");
            return errors.Count == 0;
        }
    }
    // 短信验证码校验
    public class SmsValidator : ILoginValidator
    {
        public bool Validate(LoginRequest request, out List<string> errors)
        {
            errors = new List<string>();
            if (string.IsNullOrEmpty(request.Phone))
                errors.Add("手机号不能为空");
            else if (!request.Phone.IsValidPhoneNumber())
                errors.Add("手机号格式不正确");
            if (string.IsNullOrEmpty(request.Code))
                errors.Add("验证码不能为空");
            return errors.Count == 0;
        }
    }
    // 微信登录校验
    public class WeChatValidator : ILoginValidator
    {
        public bool Validate(LoginRequest request, out List<string> errors)
        {
            errors = new List<string>();
            if (string.IsNullOrEmpty(request.WeChatCode))
                errors.Add("微信授权码不能为空");
            return errors.Count == 0;
        }
    }
    public class ValidatorFactory
    {
        private readonly Dictionary<LoginWayEnum, ILoginValidator> _validators;

        public ValidatorFactory()
        {
            _validators = new Dictionary<LoginWayEnum, ILoginValidator>
        {
            { LoginWayEnum.Pwd, new PasswordValidator() },
            { LoginWayEnum.Sms, new SmsValidator() },
            { LoginWayEnum.Wechat, new WeChatValidator() }
        };
        }

        public ILoginValidator GetValidator(LoginWayEnum type)
        {
            return _validators[type];
        }
    }
}
